package likou.offer2;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/09/08 13:22
 */
public class _001 {
    public static void main(String[] args) {
        _001 o = new _001();
        System.out.println(Integer.toBinaryString(29));
        System.out.println(Integer.toBinaryString(2));
        System.out.println(o.divide(29, 2));
    }

    public int divide(int a, int b) {
        boolean isNeg = false;
        long x = a, y = b;
        if ((a < 0 && b > 0) || (a > 0 && b < 0)) isNeg = true;
        if (x < 0) x = -x;
        if (y < 0) y = -y;
        long ans = 0;
        while (x >= y) {
            long base = y, cnt = 1;
            while (x >= base) {
                base <<= 1;
                cnt <<= 1;
            }
            base >>= 1;
            cnt >>= 1;
            x -= base;
            ans += cnt;
        }
        ans = isNeg ? -ans : ans;
        if (ans > Integer.MAX_VALUE || ans < Integer.MIN_VALUE) return Integer.MAX_VALUE;
        return (int) ans;
    }
}
